# -*- coding:utf-8 -*-
# @Author:🎈RedBalloon
# @Time:2022/9/30-11:29
# @File:10-proxy代理的使用2.py
from urllib.request import Request, build_opener
from urllib.request import ProxyHandler
from fake_useragent import UserAgent

# url = 'http://www.baidu.com'
# 查看自己网页的ip地址: https://ip.cn/
# 测试网址 http://httpbin.org/get
url = "http://httpbin.org/get"
headers ={
    "User-Agent": UserAgent().random
}

request = Request(url, headers=headers)

# 传入代理
# https://proxy.seofangfa.com/
handler = ProxyHandler({"HTTP": "183.233.169.226:9091"})
# 付费的话自己查看对应网址的ip使用说明
# handler = ProxyHandler({"HTTP": "username:password@ip:port"})
opener = build_opener(handler)

response = opener.open(request, timeout=3)

print(response.read().decode())